package com.tky.conference.utlils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by yy on 2015/10/27.
 */
public class LoginSP {

    private final String KEY_LOGIN_NAME="loginName";
    private final String KEY_PWD="pwd";

    private  SharedPreferences sharedPreferences;
    private static LoginSP mloginSP;
    private final String fileName = "login";

    private LoginSP(Context context){
        sharedPreferences=context.getSharedPreferences(fileName,context.MODE_PRIVATE);
    }

    public static LoginSP getInstance(Context context){
        if (null==mloginSP){
            mloginSP=new LoginSP(context);
        }
        return mloginSP;
    }

    public void saveLoginInfo(String username,String password){

        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(KEY_LOGIN_NAME,username);
        editor.putString(KEY_PWD,password);
        editor.commit();
    }

    public SpLoginIdentity getLoginIdentity(){
        String username=sharedPreferences.getString(KEY_LOGIN_NAME, null);
        String password=sharedPreferences.getString(KEY_PWD,null);
        if (TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            return null;
        }else {
            return new SpLoginIdentity(username,password);
        }

    }
    public void clearLoginInfo(){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(KEY_LOGIN_NAME);
        editor.remove(KEY_PWD);
        editor.commit();
    }

    public boolean isLoginInfoExist(){
        return !(null==getLoginIdentity());
    }

    public class SpLoginIdentity{
        private String loginName;
        private String pwd;

        public SpLoginIdentity(String mUserName,String mPwd){
            loginName = mUserName;
            pwd = mPwd;
        }

        public String getLoginName() {
            return loginName;
        }

        public String getPwd() {
            return pwd;
        }
    }
}
